/*
 * Copyright (c) 2007 IJO Technologies Ltd.
 * www.ijotechnologies.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * IJO Technologies ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with IJO Technologies.
 */

package com.nervenets.web.hibernate;

import com.nervenets.utils.JodaUtils;
import org.hibernate.annotations.Index;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;


@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class DomainObject implements Serializable {
    protected static final long serialVersionUID = 4143501351736586769L;
    @javax.persistence.Version
    @Column(name = "v")
    protected int version = 0;
    @Column(name = "rvd")
    protected int removed = 0;
    @Id
    @TableGenerator(name = "tableGenerator", initialValue = 1000)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "tableGenerator")
    private Long id;
    @Index(name = "_idx_ct")
    @Column(name = "ct")
    private int createTime = JodaUtils.getTimestamp();

    public boolean isNew() {
        return getId() == null;
    }

    public Date getCreateDateTime() {
        return JodaUtils.parseTimestamp(this.createTime);
    }

    //*********** GETTER / SETTER *********//

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public int getCreateTime() {
        return createTime;
    }

    public void setCreateTime(int createTime) {
        this.createTime = createTime;
    }

    public long getUniqueKeyValue() {
        return id == null ? 0L : id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof DomainObject)) return false;

        DomainObject that = (DomainObject) o;

        if (!getId().equals(that.getId())) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    public int getRemoved() {
        return removed;
    }

    public void setRemoved(int removed) {
        this.removed = removed;
    }
}
